<template>
  <div style="display: inline-block">
    <el-dialog
      :title="getFormTitle()"
      :visible.sync="dialog"
      width="50%"
      @close="closeDialog"
      center>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="address">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="submitMethod">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: "newDialog",
  data() {
    return {
      form: {
        username: '',
        address: '',
        status: 1
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ]
      },
      options: [{
        value: 0,
        label: '禁用'
      }, {
        value: 1,
        label: '正常'
      }, {
        value: 2,
        label: '待审核'
      }, {
        value: 3,
        label: '异常'
      }, {
        value: 4,
        label: '未知'
      }],
      formLabelWidth: '120px',
      dialog: false,
      formLoading: false,
      title: '',
      isAdd: true
    }
  },
  methods: {
    closeDialog(){
      this.$refs['form'].resetFields();
    },
    getFormTitle() {
      return `${this.title}`
    },
    submitMethod() {
      if (this.$refs['form']) {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.isAdd) {
              this.addMethod()
            } else this.editMethod()
          }
        })
      }
    },
    addMethod() {
      const data = {
        id: '',
        username: this.form.username,
        address: this.form.address,
        status: this.form.status
      }
      axios.post("http://192.168.70.91:8088/my-server/user/adduser",data).then(res => {
        if(res.data.succ){
          this.$notify({
            title: '成功',
            message: '新增成功',
            type: 'success'
          });
          this.form = {}
          this.$emit('save') //触发父组件的save事件
          this.dialog = false
        }else{
          this.$notify.error({
            title: '错误',
            message: '新增失败'
          });
        }
      })
    },
    editMethod(){
      const data = {
        id: this.form.id,
        username: this.form.username,
        address: this.form.address,
        status: this.form.status
      }
      axios.post("http://192.168.70.91:8088/my-server/user/updateuser",data).then(res => {
        if(res.data.succ){
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success'
          });
          this.form = {}
          this.$emit('save') //触发父组件的save事件
          this.dialog = false
        }else{
          this.$notify.error({
            title: '错误',
            message: '更新失败'
          });
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
